<html>
<head><meta charset="utf-8"><title>Limits of constness · t-compiler/const-eval · Zulip Chat Archive</title></head>
<h2>Stream: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/index.html">t-compiler/const-eval</a></h2>
<h3>Topic: <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html">Limits of constness</a></h3>

<hr>

<base href="https://rust-lang.zulipchat.com">

<head><link href="https://rust-lang.github.io/zulip_archive/style.css" rel="stylesheet"></head>

<a name="202359547"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202359547" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202359547">(Jun 29 2020 at 19:49)</a>:</h4>
<p>To expand on my previous question, I'm sure this something yous are thinking about too, but what do yous see as the limits to constness, for example in a hypothetical future would be possible to write the following example or is that too far?</p>
<div class="codehilite"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">JSON</span>: <span class="kp">&amp;</span><span class="kt">str</span> <span class="o">=</span><span class="w"> </span><span class="n">std</span>::<span class="n">fs</span>::<span class="n">read_to_string</span><span class="p">(</span><span class="s">&quot;./file.json&quot;</span><span class="p">).</span><span class="n">unwrap</span><span class="p">();</span><span class="w"></span>
</code></pre></div>



<a name="202359722"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202359722" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> bjorn3 <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202359722">(Jun 29 2020 at 19:51)</a>:</h4>
<p>I don't think that will happen. The soundness of the type system depends on const-eval being deterministic. Reading a file from the disk is not.</p>



<a name="202359778"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202359778" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202359778">(Jun 29 2020 at 19:51)</a>:</h4>
<p>Relevant: <a href="https://github.com/rust-lang/const-eval/issues/44">https://github.com/rust-lang/const-eval/issues/44</a></p>



<a name="202361499"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202361499" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jonas Schievink  [he/him] <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202361499">(Jun 29 2020 at 20:06)</a>:</h4>
<p>I've seen people talk about <em>writing</em> to files even</p>



<a name="202362265"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202362265" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202362265">(Jun 29 2020 at 20:14)</a>:</h4>
<blockquote>
<p>writing to files</p>
</blockquote>
<p>Just pull a page from Miri's book and make all these functions return an acceptable-but-not-what-you-expect value, like always <code>Err</code> ;-)</p>



<a name="202364546"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202364546" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> pachi <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202364546">(Jun 29 2020 at 20:35)</a>:</h4>
<p>or maybe use the proposed <code>unconst</code> to be able to do it, as if it were some sort of unsafe for const?</p>



<a name="202366575"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202366575" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202366575">(Jun 29 2020 at 20:53)</a>:</h4>
<p><span class="user-mention silent" data-user-id="211727">Jonas Schievink</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Limits.20of.20constness/near/202361499">said</a>:</p>
<blockquote>
<p>I've seen people talk about <em>writing</em> to files even</p>
</blockquote>
<p>We can't have allow writing and reading to the same during const-eval. Otherwise you would be able to observe the order in which constants are evaluated.</p>



<a name="202366861"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202366861" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Dylan MacKenzie (ecstatic-morse) <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202366861">(Jun 29 2020 at 20:55)</a>:</h4>
<p><span class="user-mention silent" data-user-id="219696">XAMPPRocky</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Limits.20of.20constness/near/202359547">said</a>:</p>
<blockquote>
<p>To expand on my previous question, I'm sure this something yous are thinking about too, but what do yous see as the limits to constness, for example in a hypothetical future would be possible to write the following example or is that too far?</p>
<div class="codehilite"><pre><span></span><code><span class="k">const</span><span class="w"> </span><span class="n">JSON</span>: <span class="kp">&amp;</span><span class="kt">str</span> <span class="o">=</span><span class="w"> </span><span class="n">std</span>::<span class="n">fs</span>::<span class="n">read_to_string</span><span class="p">(</span><span class="s">&quot;./file.json&quot;</span><span class="p">).</span><span class="n">unwrap</span><span class="p">();</span><span class="w"></span>
</code></pre></div>


</blockquote>
<p>I assume the real use-case is more complex, but it's worth noting that this is just <code>include_str</code>.</p>



<a name="202381101"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202381101" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Lokathor <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202381101">(Jun 29 2020 at 23:48)</a>:</h4>
<p>Yeah this came up in another thread here on the weekend.</p>
<p>For almost all common cases like this you can <code>include[_str|_bytes]!</code> your file data and then const process that into whatever.</p>



<a name="202386881"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202386881" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Félix Fischer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202386881">(Jun 30 2020 at 01:34)</a>:</h4>
<p>How important is the use-case of being able to read a file during compilation?</p>



<a name="202386892"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202386892" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Félix Fischer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202386892">(Jun 30 2020 at 01:35)</a>:</h4>
<p>Because you could think of... maybe a binary that you could compile with a set configuration</p>



<a name="202386896"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202386896" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Félix Fischer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202386896">(Jun 30 2020 at 01:35)</a>:</h4>
<p>So that it didn't have to do it on startup otherwise</p>



<a name="202398438"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202398438" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202398438">(Jun 30 2020 at 06:09)</a>:</h4>
<p><span class="user-mention silent" data-user-id="224471">Lokathor</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Limits.20of.20constness/near/202381101">said</a>:</p>
<blockquote>
<p>For almost all common cases like this you can <code>include[_str|_bytes]!</code> your file data and then const process that into whatever.</p>
</blockquote>
<p>That. The only use case for reading files during const eval instead of during parsing is to be able to compute which files to import. So if you want to import all of <code>foo*.txt</code> where <code>*</code> is <code>0..100</code>, then doing that with <code>include_*!</code> is a pain</p>



<a name="202415160"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202415160" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> XAMPPRocky <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202415160">(Jun 30 2020 at 09:55)</a>:</h4>
<p>Yeah to be clear I just was using that as simple example of "something that touches the filesystem". I'm more interested in knowing how much subset of Rust do people see const-eval being and where will I still have to resort to a procedural macro?</p>



<a name="202426840"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202426840" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202426840">(Jun 30 2020 at 12:19)</a>:</h4>
<p>Do you really want to have a look at the horrors I have in mind?</p>



<a name="202426866"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202426866" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> mental <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202426866">(Jun 30 2020 at 12:19)</a>:</h4>
<p><span class="user-mention silent" data-user-id="124288">oli</span> <a href="#narrow/stream/146212-t-compiler.2Fconst-eval/topic/Limits.20of.20constness/near/202426840">said</a>:</p>
<blockquote>
<p>Do you really want to have a look at the horrors I have in mind?</p>
</blockquote>
<p>by default the answer to this is yes</p>



<a name="202426958"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202426958" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202426958">(Jun 30 2020 at 12:20)</a>:</h4>
<p>So... I'll post some random things that come to mind, not sure if I've written them down anywhere in a coherent fashion</p>



<a name="202427192"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202427192" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202427192">(Jun 30 2020 at 12:23)</a>:</h4>
<p>My favorite "you currently need a proc macro and crazy trait bounds for this" feature that const eval could support is <em>declaring types</em>. So basically I want to be able to programattically place fields at any point and invent new enum optimizations. I want const eval to be able to emit a struct describing types and use <code>type Foo = const { ... };</code> syntax for it.</p>



<a name="202427294"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202427294" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202427294">(Jun 30 2020 at 12:24)</a>:</h4>
<p>In its simplest form this will allow you to create <code>NonZeroU8</code> in user code without it being a newtype tuple struct around <code>u8</code>.</p>



<a name="202427324"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202427324" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202427324">(Jun 30 2020 at 12:24)</a>:</h4>
<p>because... "obviously" I'll want to be able to specify valid ranges of values that values of the resulting type can use</p>



<a name="202427402"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202427402" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202427402">(Jun 30 2020 at 12:25)</a>:</h4>
<p>The one thing that I don't see happening is the creation of traits or trait impls (or impls in general). I'm not sure what const eval could ever bring to the table there that proc macros don't already support nicely.</p>



<a name="202427574"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202427574" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202427574">(Jun 30 2020 at 12:27)</a>:</h4>
<p>side note: about any of these, if you're interested in more discussion about it in detail, open a new topic in this stream</p>



<a name="202427640"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202427640" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202427640">(Jun 30 2020 at 12:28)</a>:</h4>
<p>I'd be happy to answer any questions and brainstorm these things</p>



<a name="202427699"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202427699" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202427699">(Jun 30 2020 at 12:29)</a>:</h4>
<p>A thing that's less const eval and more sandboxing of proc macros would be to run proc macros via miri, but that use case may be better covered by using wasm, as that is faster.</p>



<a name="202428265"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202428265" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> oli <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202428265">(Jun 30 2020 at 12:34)</a>:</h4>
<p>Not sure if this counts as CTFE, but it's related: You can use macros to create pattern aliases that still allow you to destructure the internal values. So <code>foo!(x @ bar!(42))</code> can be  a valid pattern and e.g. destructure to <code>Meh::Foo(x @ Bek::Bar(42, "true"))</code> or whatever. The diagnostics around that are a bit wanky and i'm not sure how easily these macros can be reused to <em>create</em> values, so <code>let x = bar!(42)</code> expanding to <code>let x = Bek::Bar(42, "true");</code> being valid for the same macro. Pattern aliases are essentially super restricted <code>const fn</code> that must satisfy the invariant that you can evaluate them backwards and forwards losslessly. So you can't have <code>pattern fn foo(x: u32, y: u32) -&gt; Foo { Foo(x + y) }</code> as you can't go from a <code>Foo</code> to <code>x</code> and <code>y</code>, but <code>pattern fn foo(x: u16, y: u16) -&gt; Foo { Foo(x as u32 + (y as u32 &lt;&lt; 16)) }</code> could get accepted.</p>



<a name="202438566"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202438566" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Jake Goulding <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202438566">(Jun 30 2020 at 13:55)</a>:</h4>
<p>Where is :eldritch-horror: when you need it...</p>



<a name="202439041"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202439041" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Wesley Wiser <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202439041">(Jun 30 2020 at 13:59)</a>:</h4>
<p>Pattern aliases ("active patterns" in F# parlance) are really nice and something I've missed occasionally when working in Rust.</p>



<a name="202464095"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202464095" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Avi Dessauer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202464095">(Jun 30 2020 at 16:51)</a>:</h4>
<p>This sounds like it's getting awfully close cost evaluation of data type generics (GHC.Generics).</p>
<p>Am I missing something?</p>



<a name="202518230"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202518230" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Félix Fischer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202518230">(Jul 01 2020 at 00:29)</a>:</h4>
<p><span class="user-mention" data-user-id="209924">@Avi Dessauer</span> do you mean like generics where one of the type parameters is a const value?</p>



<a name="202518295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202518295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Félix Fischer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202518295">(Jul 01 2020 at 00:30)</a>:</h4>
<p>If that's what you mean, that's being worked on at the moment ^^ look up the RFC number 2000</p>



<a name="202518347"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202518347" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Félix Fischer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202518347">(Jul 01 2020 at 00:31)</a>:</h4>
<p>It's unstable and not feature-complete yet as far as I know, but some things in the stdlib have been using it for a while now (like arrays!)</p>



<a name="202521295"></a>
<h4><a href="https://rust-lang.zulipchat.com#narrow/stream/146212-t-compiler/const-eval/topic/Limits%20of%20constness/near/202521295" class="zl"><img src="https://rust-lang.github.io/zulip_archive/assets/img/zulip.svg" alt="view this post on Zulip" style="width:20px;height:20px;"></a> Avi Dessauer <a href="https://rust-lang.github.io/zulip_archive/stream/146212-t-compiler/const-eval/topic/Limits.20of.20constness.html#202521295">(Jul 01 2020 at 01:19)</a>:</h4>
<p>No, data type generic programing is a way of encoding arbitrary types in a uniform manner.</p>
<p>While there are various encodings the key insight is all safe (not unions) types can be represented by a composition of a single sum type <code>A | B</code> and a product type  <code>(A, B)</code>.</p>
<p>Rust enums are sums types. To encode a enum with 3+ variants as a composition of <code>A | B</code> the first variant becomes <code>A</code> while <code>B</code> is a nested sum of where a is the second variant. </p>
<p>Product types with arbitrarily many fields (rust structs and tuples) are encoded using the same nesting trick.</p>
<p>Meta data may be attached to the encoding denoting type names and other information.<br>
While this encoding is useful onto it's self for implementing data type generic functions and impls (much nicer than, template Haskell the equivalent of proc macros), an equivalent non nested encoding is more intuitive. In Haskell the equivalent encoding comes at a cost.</p>
<p>With const generics it would be fully evaluated at compile time.</p>
<p>Nested fast representation<br>
<a href="https://markkarpov.com/tutorial/generics.html">https://markkarpov.com/tutorial/generics.html</a></p>
<p>Nicer representation<br>
<a href="https://hackage.haskell.org/package/generics-sop-0.5.1.0/docs/Generics-SOP.html">https://hackage.haskell.org/package/generics-sop-0.5.1.0/docs/Generics-SOP.html</a></p>
<p>The only attempt at data generics in rust<br>
<a href="https://github.com/lloydmeta/frunk#generic">https://github.com/lloydmeta/frunk#generic</a></p>



<hr><p>Last updated: Aug 07 2021 at 22:04 UTC</p>
</html>